/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.dspl.validate.violation;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.dspl.validate.ItemReference;

import java.io.Serializable;
import java.util.List;

/**
 * @author Shardul Deo
 */
public abstract class Violation implements Serializable {

  private static final long serialVersionUID = 5069610683751645070L;

  public enum Severity {
    CRITICAL,
    ERROR,
    WARNING
  }

  // Non final fields for GWT serialization
  private Severity severity;
  private List<ItemReference> locations;

  // For GWT serialization
  @SuppressWarnings("unused")
  private Violation() {
  }

  protected Violation(Severity severity, ItemReference primaryLocation) {
    this(severity, primaryLocation, ImmutableList.<ItemReference>of());
  }

  protected Violation(Severity severity,
      ItemReference primaryLocation, List<ItemReference> locations) {
    this.severity = Preconditions.checkNotNull(severity);

    ImmutableList.Builder<ItemReference> locationsBuilder = ImmutableList.builder();
    locationsBuilder.add(checkNotNull(primaryLocation));
    locationsBuilder.addAll(locations);
    this.locations = locationsBuilder.build();
  }

  public Severity getSeverity() {
    return severity;
  }

  public List<ItemReference> getLocations() {
    return locations;
  }

  /**
   * Returns the default message template suitable for {@link String#format}.
   */
  public abstract String getDefaultTemplate();

  /**
   * Returns the list of parameters for the default message template.
   */
  public abstract List<Object> getDefaultTemplateParameters();

  public abstract <T> T accept(ViolationVisitor<T> visitor);
}
